﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo_MVVM基本框架.Service
{
    class CsINIofKeyBinding : CsINI
    {
        #region 建立单例

        private static CsINIofKeyBinding uniqueInstance;
        private static readonly object locker = new object();
        public static CsINIofKeyBinding GetInstance()
        {
            if (uniqueInstance == null)
            {
                lock (locker)
                {
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new CsINIofKeyBinding();
                    }
                }
            }
            return uniqueInstance;
        }

        #endregion

        public CsINIofKeyBinding()
        {
            if (!Directory.Exists(strFileFolder))
                Directory.CreateDirectory(strFileFolder);
            if (!File.Exists(strInipath))
            {
                FileStream Fs = new FileStream(strInipath, FileMode.Create, FileAccess.ReadWrite);//创建写入文件
                Debug.WriteLine(strIniName + "文件建立成功");
                Fs.Close();
            }
            else
                Debug.WriteLine(strIniName + "文件已经存在");
        }

        private static string strIniName = "按键板绑定码.ini";
        private string strInipath = strFileFolder + strIniName;
        private static string strFileFolder = AppDomain.CurrentDomain.BaseDirectory + "CompareParm\\";

        #region ini默认写出函数

        public void WriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, strInipath);
        }

        #endregion

        #region ini默认读入为True/1

        public string ReadDefaultTrueValue(string section, string key)
        {
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            if (User.ToString() == "")
            {
                WritePrivateProfileString(section, key, "1", strInipath);
                GetPrivateProfileString(section, key, "", User, 512, strInipath);
            }
            return User.ToString();
        }

        #endregion

        #region ini默认读入为False/0

        public string ReadDefaultFalseValue(string section, string key)
        {
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            if (User.ToString() == "")
            {
                WritePrivateProfileString(section, key, "0", strInipath);
                GetPrivateProfileString(section, key, "", User, 512, strInipath);
            }
            return User.ToString();
        }

        #endregion

        #region 读取MES

        public string ReadMESValue(string section, string key)
        {
            StringBuilder User = new StringBuilder(512);
            GetPrivateProfileString(section, key, "", User, 512, strInipath);
            return User.ToString();
        }

        public string Read4CValue(string section, string key)
        {
            StringBuilder User = new StringBuilder(2048);
            GetPrivateProfileString(section, key, "", User, 2048, strInipath);
            if (User.ToString() == "")
            {
                WritePrivateProfileString(section, key, "", strInipath);
                GetPrivateProfileString(section, key, "", User, 2048, strInipath);
            }
            return User.ToString();
        }

        #endregion
    }
}
